﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PageFilter.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Basic set of filters abailable to all queryable lists
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Filters
{
    using System.Linq;

    /// <summary>
    /// Basic set of filters abailable to all queryable lists
    /// </summary>
    public static class PageFilter
    {
        /// <summary>
        /// Pager method to create pages for IQueryable collection.
        /// </summary>
        /// <typeparam name="T">Type of IQueryable collection</typeparam>
        /// <param name="collection">Collection to operate on</param>
        /// <param name="pageNo">Number of page to get starting at 1</param>
        /// <param name="pageSize">Number of records per page. Minimum size for a page is 1</param>
        /// <returns>Records from the requested page from the given collection</returns>
        public static IQueryable<T> Page<T>(this IQueryable<T> collection, int? pageNo, int? pageSize)
        {
            if (pageNo.HasValue && pageSize.HasValue)
            {
                return (from item in collection
                        where pageNo.Value > 0 && pageSize.Value > 0
                        select item).Skip(pageSize.Value * (pageNo.Value - 1)).Take(pageSize.Value);
            }

            return collection;
        }
    }
}